package com.zmn.plat.admin.controller.contract.payment.record;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.common.dictionary.ContractPaymentStageEnum;
import com.zmn.plat.model.entity.contract.payment.record.ContractPaymentRecord;
import com.zmn.plat.model.entity.contract.payment.record.ContractPaymentRecordQuery;

/**
 * 合同收付记录表
 *
 * @author linfeng
 *
 * @since 2020-05-09 11:41:45
 */
@RestController
@RequestMapping("/contract/payment/record/")
public class ContractPaymentRecordModifyController extends ContractPaymentRecordBaseController {

    /**
     * 新增修改
     * 
     * @author linfeng
     * @param contractPaymentRecord
     * @since 2020-05-09 11:41:45
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/contract/payment/record/addEdit.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ContractPaymentRecord contractPaymentRecord) {
        Date now = DateUtil.getNow();
        String opertor = getMcStaffRealName();
        Integer relateId = contractPaymentRecord.getRelateId();

        ContractPaymentRecordQuery contractPaymentRecordQuery = new ContractPaymentRecordQuery();
        contractPaymentRecordQuery.setStage(contractPaymentRecord.getStage());
        contractPaymentRecordQuery.setContractId(contractPaymentRecord.getContractId());
        List<ContractPaymentRecord> contractPaymentRecords = contractPaymentRecordService.listByQuery(contractPaymentRecordQuery);

        try {
            if (relateId == null || relateId == 0) {
                if (contractPaymentRecords != null && contractPaymentRecords.size() > 0) {
                    return ResponseDTO
                        .fail(String.format("已经存在[%s]记录！", ContractPaymentStageEnum.of(contractPaymentRecord.getStage()).getName()));
                }
                contractPaymentRecord.setCreater(opertor);
                contractPaymentRecord.setCreateTime(now);
                contractPaymentRecord.setUpdater(opertor);
                contractPaymentRecord.setUpdateTime(now);
                contractPaymentRecordService.insert(contractPaymentRecord);
            } else {
                if (contractPaymentRecords != null && contractPaymentRecords.size() > 0) {
                    if (contractPaymentRecords.size() > 1
                        || !Objects.equals(contractPaymentRecords.get(0).getRelateId(), contractPaymentRecord.getRelateId())) {
                        return ResponseDTO
                            .fail(String.format("已经存在[%s]收付记录！", ContractPaymentStageEnum.of(contractPaymentRecord.getStage()).getName()));
                    }
                }
                contractPaymentRecord.setUpdater(opertor);
                contractPaymentRecord.setUpdateTime(now);
                contractPaymentRecordService.updateByKey(contractPaymentRecord);
            }

            return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "操作成功！");
        } catch (Exception ex) {
            logger.error("新增/修改合同收付记录失败：contractPaymentRecord:{}", contractPaymentRecord, ex);
            return ResponseDTO.fail("操作失败");
        }
    }
}
